//
//  CustomerTabBarController.m
//  TapController
//
//  Created by marcus on 11-10-10.
//  Copyright (c) 2011年 wangjun.easymrse.com. All rights reserved.
//

#import "CustomerTabBarController.h"

#import "TapTabBarItemView.h"

#import "UIViewController1.h"

#import "UIViewController2.h"

#import "UIViewController3.h"

#import "UIViewController4.h"

@implementation CustomerTabBarController

@synthesize tabBarArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)loadView
{
    [super loadView];
    //添加底层背景色
    [self.view setFrame:CGRectMake(0, 0, 320, 460)];
    contentView=[[[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 460-48)]autorelease];
    [self.view addSubview:contentView];
    
    UIView *tabBarView=[[[UIView alloc] initWithFrame:CGRectMake(0, 460-48, 320, 48)] autorelease];
    [self.view addSubview:tabBarView];
    UIImageView *backGroundView=[[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tabBar_background.png"]] autorelease];
    [tabBarView addSubview:backGroundView];
    
    //添加tabbar
    TapTabBarItemView *tapTabBarItemView=[[[TapTabBarItemView alloc] init] autorelease];
    [tapTabBarItemView setTitle:@"会议室" imageName:@"tabBar_1.png" tag:0];
    [tabBarView addSubview:tapTabBarItemView];
    
    TapTabBarItemView *tapTabBarItemView1=[[[TapTabBarItemView alloc] init] autorelease];
    [tapTabBarItemView1 setTitle:@"通讯录" imageName:@"tabBar_2.png" tag:1];
    [tabBarView addSubview:tapTabBarItemView1];
    
    TapTabBarItemView *tapTabBarItemView2=[[[TapTabBarItemView alloc] init] autorelease];
    [tapTabBarItemView2 setTitle:@"会议记录" imageName:@"tabBar_3.png" tag:2];
    [tabBarView addSubview:tapTabBarItemView2];
    
    TapTabBarItemView *tapTabBarItemView3=[[[TapTabBarItemView alloc] init] autorelease];
    [tapTabBarItemView3 setTitle:@"更多" imageName:@"tabBar_4.png" tag:3];
    [tabBarView addSubview:tapTabBarItemView3];
    
    [self performSelector:@selector(initTabBarArray)];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"change.tabbar.view" object:[NSNumber numberWithInt:0]];
}
-(void)initTabBarArray
{
    //添加监听器
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTabBarView:) name:@"change.tabbar.view" object:nil];
    self.tabBarArray=[[NSMutableArray alloc] init];
    UIViewController1 *meetingRoomViewController =[[UIViewController1 alloc] initWithNibName:nil bundle:nil];
    UINavigationController *navController1 =[[[UINavigationController alloc] initWithRootViewController:meetingRoomViewController] autorelease];
    navController1.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    navController1.title=@"会议室";
    [meetingRoomViewController release];
    
    UIViewController2 *contactsBarViewController =[[UIViewController2 alloc] initWithNibName:nil bundle:nil];
    contactsBarViewController.title=@"通讯录";
    UINavigationController *navController2 = [[[UINavigationController alloc] initWithRootViewController:contactsBarViewController] autorelease];
    navController2.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    navController2.title=@"通讯录";
    [contactsBarViewController release];
    
    UIViewController3 *minutesViewController =[[UIViewController3 alloc] initWithNibName:nil bundle:nil];
    minutesViewController.title=@"会议记录";    
    UINavigationController *navController3 = [[[UINavigationController alloc] initWithRootViewController:minutesViewController] autorelease];
    navController3.title=@"会议记录";
    navController3.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    [minutesViewController release];
    
    UIViewController4 *moreControllerView =[[UIViewController4 alloc] initWithNibName:nil bundle:nil];
    moreControllerView.title=@"更多";
    UINavigationController *navController4 = [[[UINavigationController alloc] initWithRootViewController:moreControllerView] autorelease];
    [navController4.navigationBar setFrame:CGRectMake(0, 0, 320, 44)];
    navController4.navigationBar.tintColor = [UIColor colorWithRed:75.0/255  green:118.0/255  blue:164.0/255  alpha:1]; 
    [moreControllerView release];
    
    NSArray *array=[NSArray arrayWithObjects:navController1,navController2,navController3,navController4,nil];
    [tabBarArray addObjectsFromArray:array];
}
-(void)changeTabBarView:(NSNotification *)notifaction
{
    int tabBarIndex=[[notifaction object] intValue];
    UINavigationController *navigationController=[self.tabBarArray objectAtIndex:tabBarIndex];
    for (UIView  *v in contentView.subviews) {
        [v removeFromSuperview];
    }
    UINavigationBar *barView=[navigationController navigationBar];
    [barView setFrame:CGRectMake(0, 0, 320, 44)];
    [navigationController popToRootViewControllerAnimated:YES];
    [contentView addSubview:navigationController.view];
}
-(void)viewWillUnload
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewWillUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)dealloc
{
    NSLog(@"CustomerTabBarController delloc");
    [self.tabBarArray release];
    [super dealloc];
}
@end
